﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Abp.Application.Services;
using Abp.IdentityFramework;
using Abp.Runtime.Session;
using shenghua.Authorization.Users;
using shenghua.MultiTenancy;
using shenghua.Member.Members;
using shenghua.Extensions;
using Abp.UI;
using Abp.Domain.Repositories;

namespace shenghua
{
    /// <summary>
    /// Derive your application services from this class.
    /// </summary>
    public abstract class shenghuaAppServiceBase : ApplicationService
    {
        public TenantManager TenantManager { get; set; }

        public UserManager UserManager { get; set; }

        public long UserId
        {
            get
            {
                if (AbpSession.UserId == null)
                {
                    throw new UserFriendlyException("会员未登录");
                }
                else return (long)AbpSession.UserId;
            }
        }

        public long MemberId
        {
            get
            {
                if (AbpSession.UserId == null)
                {
                    throw new UserFriendlyException("会员未登录");
                }
                else return (long)AbpSession.UserId;
            }
        }

        public long AdminId
        {
            get
            {
                if (AbpSession.UserId == null)
                {
                    throw new UserFriendlyException("管理员未登录");
                }
                else return (long)AbpSession.UserId;
            }
        }

        public long? MemberIdOrNull
        {
            get
            {
                if (AbpSession.UserId == null) return null;
                else return (long)AbpSession.UserId;
            }
        }

        protected shenghuaAppServiceBase()
        {
            LocalizationSourceName = shenghuaConsts.LocalizationSourceName;
        }

        protected virtual async Task<Member.Members.Member> GetCurrentMemberAsync(bool mustHasMember = true)
        {
            Member.Members.Member member = null;

            if (AbpSession.UserId != null)
            {
                var user = await UserManager.FindByIdAsync(AbpSession.GetUserId().ToString());
                member = user as Member.Members.Member;
            }

            if (mustHasMember && member == null)
            {
                throw new UserFriendlyException("当前会员不存在!");
            }
            else
            {
                return member;
            }
        }

        protected virtual Task<User> GetCurrentUserAsync()
        {
            var user = UserManager.FindByIdAsync(AbpSession.GetUserId().ToString());
            if (user == null)
            {
                throw new UserFriendlyException("当前用户不存在!");
            }

            return user;
        }

        protected virtual Task<Tenant> GetCurrentTenantAsync()
        {
            return TenantManager.GetByIdAsync(AbpSession.GetTenantId());
        }

        protected virtual void CheckErrors(IdentityResult identityResult)
        {
            identityResult.CheckErrors(LocalizationManager);
        }
    }
}
